#include "dlg_login.h"
#include "ui_dlg_login.h"
#include <QDebug>

dlg_login::dlg_login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::dlg_login)
{
    ui->setupUi(this);
}

dlg_login::~dlg_login()
{
    delete ui;
}

void dlg_login::on_btn_StuLogin_clicked()
{
    //初始化
    ui->warningName->setText("用户名不能为空");
    ui->warningPwd->setText("密码不能为空");
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::red);
    ui->warningName->setPalette(pe);
    ui->warningPwd->setPalette(pe);
    //默认不可见
    ui->warningName->setVisible(false);
    ui->warningPwd->setVisible(false);
    //获取表格内容
    QString user =ui->EditName->text();
    QString pwd = ui->EditPassword->text();
    if(user=="")
    {
        ui->warningName->setVisible(true);
        return;
    }
    if(pwd=="")
    {
        ui->warningPwd->setVisible(true);
        return;
    }

    //数据库查找账号和密码
    QString str = "select * from student";
    auto ptr = supsql::getinstance();
    QSqlQuery query;
    query.exec(str);
    if(ptr->getStuCnt()==0)
    {
        QMessageBox::warning(nullptr,"error","数据库为空");
        return;
    }
    while (query.next())
    {
        QString SqlName = query.value(2).toString();
        QString SqlPwd = query.value(3).toString();
        if(user == SqlName && pwd == SqlPwd)
        {
            QMessageBox::information(nullptr,"OK","学生登录");
            emit loginSucceeded();//成功就进入主界面
            this->hide();
            return;
        }
    }
    QMessageBox::warning(nullptr,"Error","用户名或密码错误");
    ui->EditName->clear();
    ui->EditPassword->clear();

}
